package cn.xxc.basic.controller;

import cn.xxc.basic.exception.BusinessException;
import cn.xxc.basic.service.IVerifyService;
import cn.xxc.basic.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.prefs.BackingStoreException;

@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {
    @Autowired
    private IVerifyService verifyService;

    @GetMapping("/image/{key}")
    public String imageVerifyCode(@PathVariable("key") String key){
        return verifyService.imageVerifyCode(key);
    }

    @PostMapping("/smsCode")
    public AjaxResult smsCode(@RequestBody Map<String,String> map){
        try {
            verifyService.phoneVerifyCode(map);
            return new AjaxResult();
        }catch (BusinessException e){
            return new AjaxResult(false, e.getMessage());
        }catch (Exception e){
            return new AjaxResult(false,"系统繁忙，请稍后重试！");
        }
    }

    @GetMapping("/binderSmsCode/{phone}")
    public AjaxResult binderSmsCode(@PathVariable("phone") String phone ){
        try {
            verifyService.binderSmsCode(phone);
            return new AjaxResult();
        }catch (BusinessException e){
            return new AjaxResult(false, e.getMessage());
        }catch (Exception e){
            return new AjaxResult(false,"系统繁忙，请稍后重试！");
        }
    }
}
